package A_review.review4_21;

public class LongestHuiWen {

    public String longestPalindrome(String s) {
        String res="";
        int length = s.length();
        //  CKG TODO 2023/4/21: 这个地方为什么是length-1呢
        for (int i = 0; i < length-1; i++) {
            String res1 = judge(s, i, i);
            String res2 = judge(s, i, i + 1);
            res=res1.length()>res.length()?res1:res;
            res=res2.length()>res.length()?res2:res;
        }
        return res;
    }

    public String judge(String s,int i,int j){
        //  CKG TODO 2023/4/21: 这个地方用while
//      while (i<j&&j<s.length()&&s.charAt(i)==s.charAt(j)&&i>=0){
          while (j<s.length()&&i>=0&&s.charAt(i)==s.charAt(j)){
              i--;
              j++;
           }
          //  CKG TODO 2023/4/21: 这个地方需要注意边界的情况
          if (i+1<j-1){
            String res=s.substring(i+1,j);
             return  res;
          }
        return "";
    }


    //  CKG TODO 2023/4/21: 判断一个字符串是不是回文
    public boolean judge(String s){
        int left=0,right=s.length()-1;
        while (left<right){
            if (s.charAt(left)==s.charAt(right)){
                left++;
                right--;
            }else {
                return  false;
            }
        }
        return  true;
    }


    //  CKG TODO 2023/4/21:  不太熟悉这个
  ListNode left;
    //  CKG TODO 2023/4/21: 判断链表是不是回文
    public boolean judge(ListNode head){
        left=head;
        return traverse(left);
    }

    boolean traverse(ListNode  right){
        if (right==null){
            return  true;
        }
//  CKG TODO 2023/4/21:  相当将链表反转 到最后一个节点
        boolean res = traverse(right.next);
        res=res&&(right.val== left.val);
        left=left.next;
        return  res;

    }






    public static void main(String[] args) {
        LongestHuiWen longestHuiWen = new LongestHuiWen();
        System.out.println(longestHuiWen.longestPalindrome("sfgbcb"));


    }






}
